{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 18\n",
    "\n",
    "### **Question:**\n",
    "\n",
    "> **_A website requires the users to input username and password to register. Write a program to check the validity of password input by users._**\n",
    "\n",
    "> **_Following are the criteria for checking the password:_**\n",
    "\n",
    "- **_At least 1 letter between [a-z]_**\n",
    "- **_At least 1 number between [0-9]_**\n",
    "- **_At least 1 letter between [A-Z]_**\n",
    "- **_At least 1 character from [$#@]_**\n",
    "- **_Minimum length of transaction password: 6_**\n",
    "- **_Maximum length of transaction password: 12_**\n",
    "\n",
    "> **_Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma._**\n",
    "\n",
    "> **_Example_**\n",
    "\n",
    "> **_If the following passwords are given as input to the program:_**\n",
    "\n",
    "\n",
    "ABd1234@1,a F1#,2w3E*,2We3345\n",
    "\n",
    "\n",
    "> **_Then, the output of the program should be:_**\n",
    "\n",
    "\n",
    "ABd1234@1\n",
    "\n",
    "\n",
    "---\n",
    "\n",
    "### Hints:\n",
    "\n",
    "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aaaaa\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def is_low(x):  # Returns True  if the string has a lowercase\n",
    "    for i in x:\n",
    "        if \"a\" <= i and i <= \"z\":\n",
    "            return True\n",
    "    return False\n",
    "\n",
    "\n",
    "def is_up(x):  # Returns True  if the string has a uppercase\n",
    "    for i in x:\n",
    "        if \"A\" <= i and i <= \"Z\":\n",
    "            return True\n",
    "    return False\n",
    "\n",
    "\n",
    "def is_num(x):  # Returns True  if the string has a numeric digit\n",
    "    for i in x:\n",
    "        if \"0\" <= i and i <= \"9\":\n",
    "            return True\n",
    "    return False\n",
    "\n",
    "\n",
    "def is_other(x):  # Returns True if the string has any \"$#@\"\n",
    "    for i in x:\n",
    "        if i == \"$\" or i == \"#\" or i == \"@\":\n",
    "            return True\n",
    "    return False\n",
    "\n",
    "\n",
    "s = input().split(\",\")\n",
    "lst = []\n",
    "\n",
    "for i in s:\n",
    "    length = len(i)\n",
    "    if (\n",
    "        6 <= length\n",
    "        and length <= 12\n",
    "        and is_low(i)\n",
    "        and is_up(i)\n",
    "        and is_num(i)\n",
    "        and is_other(i)\n",
    "    ):  # Checks if all the requirments are fulfilled\n",
    "        lst.append(i)\n",
    "\n",
    "print(\",\".join(lst))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**OR**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def check(x):\n",
    "    cnt = 6 <= len(x) and len(x) <= 12\n",
    "    for i in x:\n",
    "        if i.isupper():\n",
    "            cnt += 1\n",
    "            break\n",
    "    for i in x:\n",
    "        if i.islower():\n",
    "            cnt += 1\n",
    "            break\n",
    "    for i in x:\n",
    "        if i.isnumeric():\n",
    "            cnt += 1\n",
    "            break\n",
    "    for i in x:\n",
    "        if i == \"@\" or i == \"#\" or i == \"$\":\n",
    "            cnt += 1\n",
    "            break\n",
    "    return (\n",
    "        cnt == 5\n",
    "    )  # counting if total 5 all conditions are fulfilled then returns True\n",
    "\n",
    "\n",
    "s = input().split(\",\")\n",
    "lst = filter(\n",
    "    check, s\n",
    ")  # Filter function pick the words from s, those returns True by check() function\n",
    "print(\",\".join(lst))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**OR**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "s = input().split(\",\")\n",
    "lst = []\n",
    "\n",
    "for i in s:\n",
    "    cnt = 0\n",
    "    cnt += 6 <= len(i) and len(i) <= 12\n",
    "    cnt += bool(\n",
    "        re.search(\"[a-z]\", i)\n",
    "    )  # here re module includes a function re.search() which returns the object information\n",
    "    cnt += bool(\n",
    "        re.search(\"[A-Z]\", i)\n",
    "    )  # of where the pattern string i is matched with any of the [a-z]/[A-z]/[0=9]/[@#$] characters\n",
    "    cnt += bool(\n",
    "        re.search(\"[0-9]\", i)\n",
    "    )  # if not a single match found then returns NONE which converts to False in boolean\n",
    "    cnt += bool(re.search(\"[@#$]\", i))  # expression otherwise True if found any.\n",
    "    if cnt == 5:\n",
    "        lst.append(i)\n",
    "\n",
    "print(\",\".join(lst))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 19\n",
    "\n",
    "### **Question:**\n",
    "\n",
    "> **_You are required to write a program to sort the (name, age, score) tuples by ascending order where name is string, age and score are numbers. The tuples are input by console. The sort criteria is:_**\n",
    "\n",
    "- **_1: Sort based on name_**\n",
    "- **_2: Then sort based on age_**\n",
    "- **_3: Then sort by score_**\n",
    "\n",
    "> **_The priority is that name > age > score._**\n",
    "\n",
    "> **_If the following tuples are given as input to the program:_**\n",
    "\n",
    "\n",
    "Tom,19,80\n",
    "\n",
    "John,20,90\n",
    "\n",
    "Jony,17,91\n",
    "\n",
    "Jony,17,93\n",
    "\n",
    "Json,21,85\n",
    "\n",
    "\n",
    "> **_Then, the output of the program should be:_**\n",
    "\n",
    "\n",
    "[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]\n",
    "\n",
    "\n",
    "---\n",
    "\n",
    "### Hints:\n",
    "\n",
    "> **_In case of input data being supplied to the question, it should be assumed to be a console input.We use itemgetter to enable multiple sort keys._**\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = []\n",
    "while True:\n",
    "    s = input().split(\",\")\n",
    "    if not s[0]:  # breaks for blank input\n",
    "        break\n",
    "    lst.append(tuple(s))\n",
    "\n",
    "lst.sort(\n",
    "    key=lambda x: (x[0], x[1], x[2])\n",
    ")  # here key is defined by lambda and the data is sorted by element priority 0>1>2 in accending order\n",
    "print(lst)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
